import { NestFactory } from '@nestjs/core'; // esModule 前后端同构
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import * as cookieParser from 'cookie-parser';
import * as mongoose from 'mongoose';

async function bootstrap() {
  mongoose.connect('mongodb://localhost:27017/nestjs', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  });
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  // 设置swagger文档
  const options = new DocumentBuilder()
    .setTitle('Nestjs博客api')
    .setDescription('测试swagger')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('doc', app, document);

  // 设置资源目录和虚拟目录
  app.useStaticAssets(join(__dirname, '..', 'public'), {
    prefix: '/static/',
  });

  // 设置cookie
  app.use(cookieParser());

  // 设置模版引擎
  app.useStaticAssets(join(__dirname, '..', 'public'));
  app.setBaseViewsDir('views');
  app.setViewEngine('hbs');

  await app.listen(3000, function() {
    console.log(`服务启动: http://localhost:3000`);
  });
}
bootstrap();

/*
https://docs.nestjs.com/techniques/mvc
中文文档
https://www.itying.com/nestjs/start-template.html
https://exlley.gitbooks.io/nest-js/content/
*/
